//
//  YCRegisterViewController.m
//  lianganyuchu
//
//  Created by iMac on 17/3/1.
//  Copyright © 2017年 杭州桑缇电子商务有限公司. All rights reserved.
//

#import "YCRegisterViewController.h"
#define ORGIN_X 15
#define BTN_HEIGHT 50
#define FIELD_HEIGHT 50

@interface YCRegisterViewController ()
{
    UITextField *m_phoneField;
    UITextField *m_codeField;
    
    UIButton *m_btnCode;
    UIButton *m_btnNext;
    
    UILabel *m_labTint;
    
    NSTimer *_timer;
    int _counter;
    BOOL _isInitWithRestPse;
    NSString *_code;
    
    CGFloat _tintCenterY;
    
    CGFloat _codeCenterY;
    CGFloat _nextCenterY;
}
@end

@implementation YCRegisterViewController
-(id)initWith:(BOOL)isResetPassword
{
    self=[super init];
    if (self) {
        _isInitWithRestPse=isResetPassword;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title=_isInitWithRestPse ? @"找回密码" : @"注册";
    self.navigationItem.leftBarButtonItem=[self tbarBackButton];
    
    _counter=60;
    [self.view setBackgroundColor:WHITE_COLOR];
    [self addMainView];
}
-(void)addMainView
{
    CGFloat btnWidth=kMainScreen_Width-2*ORGIN_X;
    
    CGFloat fWidth=btnWidth;
    m_phoneField=[[UITextField alloc]initWithFrame:CGRectMake(ORGIN_X, 80, fWidth, FIELD_HEIGHT)];
    UIView *leftView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, kDistance, FIELD_HEIGHT)];
    m_phoneField.leftView = leftView;
    m_phoneField.leftViewMode = UITextFieldViewModeAlways;
    [m_phoneField setBackgroundColor:WHITE_COLOR];
    [m_phoneField setFont:[UIFont LightFontOfSize:15]];
    [m_phoneField setPlaceholder:@"请输入您的手机号码" Corlor:DISABLE_COLOR];
    m_phoneField.delegate=self;
    m_phoneField.keyboardType=UIKeyboardTypeNumberPad;
    m_phoneField.clearButtonMode=UITextFieldViewModeWhileEditing;
    [self.view addSubview:m_phoneField];
    [m_phoneField addShadow];
    
    m_labTint=[[UILabel alloc]initWithFrame:CGRectMake(ORGIN_X, CGRectGetMaxY(m_phoneField.frame)+7, fWidth, 20)];
    [m_labTint setTextColor:MAIN_COLOR];
    [m_labTint setFont:[UIFont LightFontOfSize:14]];
    [self.view addSubview:m_labTint];
    [m_labTint setHidden:YES];
    _tintCenterY=m_labTint.center.y;
    
    
    m_codeField=[[UITextField alloc]initWithFrame:CGRectMake(ORGIN_X, CGRectGetMaxY(m_phoneField.frame)+10, fWidth, FIELD_HEIGHT)];
    [m_codeField setFont:[UIFont LightFontOfSize:15]];
    [m_codeField setBackgroundColor:WHITE_COLOR];
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, kDistance, FIELD_HEIGHT)];
    m_codeField.leftView = view;
    m_codeField.leftViewMode = UITextFieldViewModeAlways;
    m_codeField.delegate=self;
    m_codeField.keyboardType=UIKeyboardTypeNumberPad;
    [m_codeField setPlaceholder:@"请输入验证码" Corlor:DISABLE_COLOR];
    [m_codeField addShadow];
    [self.view addSubview:m_codeField];
    
    m_btnCode=[[UIButton alloc]initWithFrame:CGRectMake(W(m_codeField)-110,0,110, H(m_codeField))];
    m_btnCode.layer.cornerRadius=2.0f;
    [m_btnCode setBackgroundColor:MAIN_COLOR];
    [m_btnCode setTitle:@"获取验证码" forState:UIControlStateNormal];
    [m_btnCode.titleLabel setFont:[UIFont LightFontOfSize:16]];
    [m_codeField addSubview:m_btnCode];
    [m_btnCode addTarget:self action:@selector(vertifyBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChange)name:UITextFieldTextDidChangeNotification object:m_phoneField];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChange)name:UITextFieldTextDidChangeNotification object:m_codeField];
    
    m_btnNext=[[UIButton alloc]initWithFrame:CGRectMake(ORGIN_X, CGRectGetMaxY(m_codeField.frame)+50, btnWidth, BTN_HEIGHT)];
    [m_btnNext.titleLabel setFont:[UIFont LightFontOfSize:18]];
    [m_btnNext setBackgroundColor:MAIN_COLOR];
    m_btnNext.layer.cornerRadius=2.0f;
    [m_btnNext setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
    [m_btnNext setTitle:@"下一步" forState:UIControlStateNormal];
    [m_btnNext addTarget:self action:@selector(btnNextClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:m_btnNext];
    
    _codeCenterY=m_codeField.center.y;
    _nextCenterY=m_btnNext.center.y;
    
    [self textChange];
}
-(void)textChange
{
    [self hideError];
    m_btnNext.enabled= (m_phoneField.text.length==11&&m_codeField.text.length==4);
    if (m_btnNext.enabled) {
        [m_btnNext setBackgroundColor:MAIN_COLOR];
    }else{
        [m_btnNext setBackgroundColor:LIGHT_COLOR];
    }
}

-(void)btnNextClicked:(UIButton *)button
{
    [m_phoneField resignFirstResponder];
    [m_codeField resignFirstResponder];
    UserFindPwdRequest *request=[UserFindPwdRequest new];
    request.phone=m_phoneField.text;
    request.code=m_codeField.text;
    [apiClient hideProgress];
    [apiClient disableAfterRequest];
    [apiClient doUserFindPwd:request success:^(ApiResponse *data, NSString *url) {
        
        YCRegisterSecondViewController *secondVC=[[YCRegisterSecondViewController alloc]initWith:YES];
        secondVC.phone=m_phoneField.text;
        secondVC.code=m_codeField.text;
        [self showNavigationView:secondVC];
        
    }failure:^(ApiResponse *data, NSString *url){
        [self showErrorWith:data.message and:NO];
    }];
}
-(void)vertifyBtnClicked:(UIButton *)button
{
    if(button.selected) return;
    button.selected=YES;
    if ([m_phoneField.text isMobile]) {
        [self sendSMSCode];
    }
    else {
        m_btnCode.selected=NO;
        [self showErrorWith:@"请输入正确的手机号码" and:YES];
    }
}
-(void)showErrorWith:(NSString *)error and:(BOOL)istele
{
   [m_labTint setHidden:NO];
   [m_labTint setText:error];
    
    [UIView animateWithDuration:0.1 animations:^{
        if (istele) {
            m_labTint.center=CGPointMake(m_labTint.center.x, _tintCenterY);
            
            m_codeField.center=CGPointMake(m_codeField.center.x, _codeCenterY+35);
            m_btnNext.center=CGPointMake(m_btnNext.center.x, _nextCenterY+35);
        }else{
            
            m_labTint.center=CGPointMake(m_labTint.center.x, _tintCenterY+60);
        }
    }];

    
}
-(void)hideError
{
    [m_labTint setHidden:YES];
    
    m_labTint.center=CGPointMake(m_labTint.center.x, _tintCenterY);
    m_codeField.center=CGPointMake(m_codeField.center.x, _codeCenterY);
    m_btnNext.center=CGPointMake(m_btnNext.center.x, _nextCenterY);
}

- (void)sendSMSCode{
    SmsSendCodeRequest *request=[SmsSendCodeRequest new];
    request.find_pwd = _isInitWithRestPse ? @"1" : @"0";
    request.phone=m_phoneField.text;
    [apiClient hideProgress];
    [apiClient disableAfterRequest];
    [apiClient doSmsSendCode:request success:^(ApiResponse *data, NSString *url) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];
        [self.view showHudWith:data.message]; 
    }failure:^(ApiResponse *data, NSString *url){
        [self showErrorWith:data.message and:NO];
        m_btnCode.selected=NO;
    }];
}

-(void)timerFireMethod:(NSTimer *)theTimer {
    if (_counter == 1) {
        [theTimer invalidate];
        _counter = 60;
        [m_btnCode setTitle:@"重新发送" forState: UIControlStateNormal];
        [m_btnCode setBackgroundColor:MAIN_COLOR];
        [m_btnCode.titleLabel setFont:[UIFont LightFontOfSize:16]];
        [m_btnCode setEnabled:YES];
    }else{
        _counter--;
        NSString *title = [NSString stringWithFormat:@"重新发送（%ds）",_counter];
        m_btnCode.selected=NO;
        [m_btnCode setEnabled:NO];
        [m_btnCode.titleLabel setFont:[UIFont LightFontOfSize:13]];
         [m_btnCode setBackgroundColor:LIGHT_COLOR];
        [m_btnCode setTitle:title forState:UIControlStateNormal];
    }
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    [self hideError];
    if (textField==m_phoneField) {
        if (range.location>10) {
            return NO;
        }
        return YES;
    }
    if (textField==m_codeField) {
        if (range.location>3) {
            return NO;
        }
        return YES;
    }
    return YES;
}
-(void)viewWillDisappear:(BOOL)animated
{
    [m_phoneField resignFirstResponder];
    [m_codeField resignFirstResponder];
}


@end







